package com.atguigu.gulimall.order.listener;

import com.atguigu.common.to.mq.SeckillOrderTo;
import com.atguigu.gulimall.order.config.AlipayTemplate;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Slf4j

@Component
public class OrderSecListener {

    @Autowired
    OrderService orderService;

    @RabbitListener(queues = "order.seckill.order.queue")
    public void listener(SeckillOrderTo seckillOrder, Message message, Channel channel) throws IOException {
        log.info("收到秒杀订单消息，准备执行单->"+seckillOrder.getOrderSn());
        try{
            orderService.createSecOrder(seckillOrder);
            //支付宝手动收单
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }
}
